// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cantor/unresolved/visitor.h>
#include <turbo/log/logging.h>

namespace cantor::unresolved {
    class UnresolvedDispatchExpr;
    class UnresolvedTableName;
    class UnresolvedStatement;
    class UnresolvedIndexHint;

    class UnresolvedTableSource : public UnresolvedVisitor<UnresolvedTableSource> {
    public:
        static constexpr const cantor::pb::ast::ExprNodeType TYPE = cantor::pb::ast::ExprNodeType::ENT_TABLE_SOURCE;

        UnresolvedTableSource(const cantor::pb::ast::TableSource &node) : instance(node) {
        }

        ~UnresolvedTableSource() override = default;

        const pb::Location &location_impl() const {
            return instance.location();
        }

        const pb::Status &error_status_impl() const {
            return instance.status();
        }

        bool has_table_name() const {
            return instance.has_table_name();
        }

        UnresolvedTableName table_name() const;

        std::string_view as_string() const {
            if (instance.has_as_name()) {
                return instance.as_name();
            }
            return std::string_view();
        }

        bool has_derived_table() const {
            return instance.has_derived_table();
        }

        UnresolvedStatement derived_table() const;

        size_t index_hint_size() const {
            return instance.index_hints_size();
        }

        std::vector<UnresolvedIndexHint> index_hints() const;
        UnresolvedIndexHint index_hint(size_t index) const;

        std::vector<std::string_view> partition_names() const {
            std::vector<std::string_view> result;
            for (size_t i = 0; i < instance.partition_names_size(); ++i) {
                result.emplace_back(instance.partition_names(i));
            }
            return result;
        }
        const cantor::pb::ast::TableSource &instance;
    };
} // namespace cantor::unresolved
